package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.dao.MemberDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import service.OrderService;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *  预约套餐业务
 */
@Transactional
@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    @Override
    public Result submit(Map map) throws Exception {

        //根据日期orderDate   查询`t_ordersetting`表,看能不能预约
       String orderDate_ori=(String) map.get("orderDate");
        Date orderDate = DateUtils.parseString2Date(orderDate_ori);
        int count=orderSettingDao.findCountByorderDate(orderDate);
       if (count==0){ //该日期不能预约
           return  new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
       }

        //日期没问题,判断预约人数是否满了  根据orderDate 查询 t_ordersetting表
        int number=orderSettingDao.findnumberByorderDate(orderDate);
        int reservations=orderSettingDao.findreservationsByorderDate(orderDate);
        if (number<=reservations){
            //预约满了
            return new Result(false,MessageConstant.ORDER_FULL);
        }

        //判断是不是会员,根据 手机号码telephone 查询t_member表 还要查出主键
        String telephone=(String)map.get("telephone");
        Integer setmealId=Integer.parseInt((String)map.get("setmealId"))  ;
        Member member=memberDao.findByPhone(telephone);
        if (member!=null){
            //是会员,那么就不能重复预约,,查询
            //member_id,orderDate,setmealId查询t_order
            Integer member_id = member.getId();
            //封装查询条件
            Order order = new Order(member_id, orderDate, null, null, setmealId);

            List<Order> list= orderDao.findByMulConditon(order);

           if (list!=null && list.size()>0){
               //不能重复约
               return new Result(false,MessageConstant.HAS_ORDERED);
           }
        }else{
            //1.向t_member表插入一条记录 成为会员 同时查出主键 返回 member_id
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.save(member);

        }

        //保存预约信息到预约表,还要返回 id
        Order order = new Order(member.getId(),
                orderDate,
                (String)map.get("orderType"),
                Order.ORDERSTATUS_NO,
                Integer.parseInt((String) map.get("setmealId")));
        orderDao.add(order);


        //2.t_ordersetting表里面预约的人数+1,根据日期  orderDate  预约人数 reservations +1
        orderSettingDao.updataReservations(map);



        return new Result(true,MessageConstant.ORDER_SUCCESS,order);

    }

    //预约后展示的信息
    @Override
    public Map findById(Integer id) throws Exception {

        Map map=orderDao.findById(id);
        //if (map!=null){
        //    Date date=(Date) map.get("orderType");
        //    map.put("orderDate",DateUtils.parseDate2String(date));
        //}

        return map;
    }
}
